//
//  MortgageController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/15.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
// 抵押贷款 //二手车买卖
class MortgageController:BaseController{
    var appTitle = ""//导航title
    var opType = 0 // 1:抵押贷款，2：二手手车买卖
    var interfaceUrl = ""
    var mortgageTable:UITableView!//数据表格
    var mCell:MortgageCell!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupNavBar(title: appTitle, isback: true, appearWay: 2, isright: false)//自定义导航
        if 1 == opType {
            interfaceUrl = "carmortgage"
        }else if (2 == opType){
            interfaceUrl = "cartrade"
        }
        //初始化表格
        mortgageTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH,width:kScreenW,height:kScreenH - kStatusBarH - kNavigationBarH),style: UITableViewStyle.plain)
        mortgageTable?.delegate = self
        mortgageTable?.dataSource = self
        mortgageTable?.separatorStyle = UITableViewCellSeparatorStyle.none
        mortgageTable?.rowHeight = kScreenH - kStatusBarH - kNavigationBarH
        self.view.addSubview(mortgageTable)
    }
}
extension MortgageController{
    //提交按钮
    func submitMortClick() {
        if "" ==  self.mCell.carType.text! {
            checkAlertFun(message: "请输入车辆类型！")
        }
        else if "" ==  self.mCell.mileage.text! {
            checkAlertFun(message: "请输入行驶里程！")
        }
        else if "" ==  self.mCell.cardCity.text! {
            checkAlertFun(message: "请输入上牌城市！")
        }
        else if "" ==  self.mCell.cardTime.text! {
            checkAlertFun(message: "请输入上牌时间！")
        }
        else if "" ==  self.mCell.carOwner.text! {
            checkAlertFun(message: "请输入车主姓名！")
        }
        else if "" ==  self.mCell.ownerPhone.text! {
            checkAlertFun(message: "请输入车主联系电话！")
        }else {
            submitMortFunc()
        }
    }
    //提交信息
    func submitMortFunc(){
        if reachability.isReachable {
            let urlString = "\(projectUrl)/order/other/commit/\(interfaceUrl)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
            let parameter:[String:Any] = [
                "orderData":"{\"carType\":\"\(self.mCell.carType.text!)\",\"mileage\":\"\(self.mCell.mileage.text!)\",\"cardCity\":\"\(self.mCell.cardCity.text!)\",\"cardTime\":\"\(self.mCell.cardTime.text!)\",\"carOwner\":\"\(self.mCell.carOwner.text!)\",\"ownerPhone\":\"\(self.mCell.ownerPhone.text!)\",\"remark\":\"\(self.mCell.remark.text!)\"}"
            ]
            Alamofire.request(urlString, method: .post, parameters: parameter, headers : headers).responseJSON { (response) in
                if response.result.isSuccess{
                    let json = JSON(response.result.value as! [String : AnyObject])
                    if json["success"] == true {
                        let alertController = UIAlertController(title: alertTitle, message: "\(self.appTitle)信息提交成功！", preferredStyle: .alert)
                        let okAction = UIAlertAction(title: "确定", style: .default, handler:{
                            (UIAlertAction) -> Void in
                            let mainVC = UINavigationController(rootViewController: MainViewController())
                            app.window?.rootViewController = mainVC
                        })
                        alertController.addAction(okAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                    else{
                        self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                    }
                }
                else{
                    print(response.result.error!) //输出错误日志
                }
            }
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
extension MortgageController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        mCell = tableView.dequeueReusableCell(withIdentifier: "mCellID") as? MortgageCell
        if mCell == nil {
            mCell = MortgageCell(style: UITableViewCellStyle.default,reuseIdentifier: "mCellID")
        }
        mCell.submitTradeBtn.setTitle("提交", for: .normal)
        mCell.submitTradeBtn.addTarget(self, action: #selector(submitMortClick), for: .touchUpInside)
        return mCell!
    }
}
